﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Collections.Generic;

using SBPweb.PhotoGallery.Common;
using SBPweb.PhotoGallery.Client.Logic;
using SBPweb.PhotoGallery.Client.Utils;

namespace SBPweb.PhotoGallery.Client
{

    public partial class SlideShow : ButtonPresenterControl
    {

        #region Dependency property definitions

        public static readonly DependencyProperty IsAutoAdvanceActiveProperty = DependencyProperty.Register("IsAutoAdvanceActive", typeof(bool), typeof(SlideShow), new PropertyMetadata(false));

        #endregion

        #region Private fields

        private AdvancingGallery gallery;

        #endregion

        #region Constructor logic

        public SlideShow()
        {
            InitializeComponent();
            InitializeProperties();
            InitializeHandlers();
        }

        protected override void InitializeProperties()
        {
            base.InitializeProperties();
            IsAutoAdvanceActive = (bool)this.GetValue(IsAutoAdvanceActiveProperty);
        }

        #endregion

        #region Property accessors

        public AdvancingGallery Gallery
        {
            get
            {
                return gallery;
            }
            set
            {
                FinalizeGallery();
                gallery = value;
                ucSlides.Gallery = value;
                InitializeGallery();
            }
        }

        public override Brush ButtonForeground
        {
            get
            {
                return base.ButtonForeground;
            }
            set
            {
                base.ButtonForeground = value;
                buttonDown.BorderBrush = value;
                buttonUp.BorderBrush = value;
            }
        }

        public override Brush ButtonBackground
        {
            get
            {
                return base.ButtonBackground;
            }
            set
            {
                base.ButtonBackground = value;
                buttonDown.Background = value;
                buttonUp.Background = value;
            }
        }

        public bool IsAutoAdvanceActive
        {
            get
            {
                return (bool)this.GetValue(IsAutoAdvanceActiveProperty);
            }
            set
            {
                this.SetValue(IsAutoAdvanceActiveProperty, value);
            }
        }

        #endregion

        #region Methods

        private void InitializeGallery()
        {
            if (gallery == null)
            {
                return;
            }
            gallery.ImageFetching += new EventHandler<SlideFetchProgressEventArgs>(ImageFetching);
            gallery.ImageFetched += new EventHandler<SlideFetchEventArgs>(ImageFetched);
            //gallery.ItemAdded += new EventHandler<OverridableList<FetchableSlide>.ItemEventArgs>(SlideAdded);
            //gallery.ItemRemoved += new EventHandler<OverridableList<FetchableSlide>.ItemEventArgs>(SlideRemoved);
        }

        private void FinalizeGallery()
        {
            if (gallery == null)
            {
                return;
            }
            gallery.ImageFetching -= new EventHandler<SlideFetchProgressEventArgs>(ImageFetching);
            gallery.ImageFetched -= new EventHandler<SlideFetchEventArgs>(ImageFetched);
            //gallery.ItemAdded -= new EventHandler<OverridableList<FetchableSlide>.ItemEventArgs>(SlideAdded);
            //gallery.ItemRemoved -= new EventHandler<OverridableList<FetchableSlide>.ItemEventArgs>(SlideRemoved);
        }

        #region Event handlers        

        private void ImageFetching(object sender, SlideFetchProgressEventArgs e)
        {
        }

        private void ImageFetched(object sender, SlideFetchEventArgs e)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                ImageBrush iB = new ImageBrush();
                BitmapImage bI = new BitmapImage();
                bI.SetSource(e.Slide.Image);
                iB.ImageSource = bI;
                gMain.Background = iB;
            });
        }

        #endregion

        #endregion

    }

}